<!-- Program:  mysql_send.php
     Desc:     PHP program that sends an SQL query to the
               MySQL server and displays the results.
-->
<?php
include '../includes/cPage.php';
include '../includes/cDates.php';
include '../includes/cDatabase.php';

$cPage = new page("mySQL Query");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

$host="localhost";
$user="root";
$password="lizardman";

/* Section that executes query */
if(@$_GET['form'] == "yes")
{
 $connect = mysqli_connect($host,$user,$password);
 mysqli_select_db($connect, $_POST['database']);
 $query = stripSlashes($_POST['query']);
 $result = mysqli_query($connect, $query);
 echo "Database Selected: <b>{$_POST['database']}</b><br>
       Query: <b>$query</b><h3>Results</h3><hr>";
 if($result == 0)
 {
    echo "<b>Error ".mysqli_errno($connect).": "
           .mysqli_error($connect)."</b>";
 }
 elseif (@mysqli_num_rows($result) == 0)
 {
    echo("<b>Query completed. No results returned.
          </b><br>");
 }
 else
 {
  echo "<table border='1'>
         <thead>
          <tr>";
           $finfo = mysqli_fetch_fields($result);
           for($i = 0;$i < mysqli_num_fields($result);$i++)
           {
            echo "<th>" .$finfo[$i]->name.
                 "</th>";
           }
  echo "  </tr>
         </thead>
        <tbody>";
         for ($i = 0; $i < mysqli_num_rows($result); $i++)
         {
           echo "<tr>";
            $row = mysqli_fetch_row($result);
            for($j = 0;$j<mysqli_num_fields($result);$j++)
            {
              echo("<td>" . $row[$j] . "</td>");
            }
           echo "</tr>";
         }
  echo "</tbody>
       </table>";
 }  //end else
 echo "
  <hr><br>
  <form action=\"{$_SERVER['PHP_SELF']}\" method=\"POST\">
    <input type='hidden' name='query' value='$query'>
    <input type='hidden' name='database'
           value={$_POST['database']}>
    <input type='submit' name=\"queryButton\"
           value=\"New Query\">
    <input type='submit' name=\"queryButton\"
           value=\"Edit Query\">
  </form>";
 unset($form);
 exit();
}  // endif form=yes

/* Section that requests user input of query */
@$query=stripSlashes($_POST['query']);
if (@$_POST['queryButton'] != "Edit Query")
{
 $query = " ";
}
?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=yes"
     method="POST">
<table>
 <tr>
  <td align=right><b>Type in database name</b></td>
  <td><input type="text" name="database"
             value=<?php echo @$_POST['database'] ?> ></td>
 </tr>
 <tr>
  <td align="right" valign="top">
        <b>Type in SQL query</b></td>
  <td><textarea name="query" cols="60"
                rows="10"><?php echo $query ?></textarea>
  </td>
 </tr>
 <tr>
  <td colspan="2" align="center"><input type="submit"
      value="Submit Query"></td>
 </tr>
</table>
</form>

<?php
echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>